<%--
  Created by IntelliJ IDEA.
  User: A415
  Date: 2024/6/20
  Time: 14:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="layui-card-header">
    下面是充数内容，为的是出现滚动条
</div>
<div class="layui-card-body">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">旧密码</label>
            <div class="layui-input-block">
                <input type="text" name="oldPassword" lay-verify="required" placeholder="请输入旧密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input type="password" name="newPassword" lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" name="rePassword" lay-verify="required" placeholder="请再次输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">确认修改</button>
            </div>
        </div>
    </form>
</div>
<script src="${pageContext.request.contextPath}/frontend/layui/axios.min.js"></script>
<script>
    //js javascript
    layui.use(['form', 'laydate', 'util'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;
        // 绑定事件
        //
        form.on('submit(demo1)', function(data){
            console.log(data.field); // 打印表单的字段
            // 执行相关的提交操作
            if(data.field.oldPassword == ""){
                layer.alert("请输入旧密码");
                return false;
            }
            if(data.field.newPassword == ""){
                layer.alert("请输入新密码");
                return false;
            }
            if(data.field.rePassword == ""){
                layer.alert("请再次输入新密码");
                return false;
            }

            if(!(data.field.newPassword == data.field.rePassword) ){
                layer.alert("两次密码不一致");
                return false;
            }
            //数据校验通过之后，post提交到服务端
            axios.post(
                '${pageContext.request.contextPath}/UserServlet136/password',
                data.field
            )
                .then(function (response) {
                    // 请求成功时处理响应数据
                    //根据服务端返回的数据做处理

                    if(response.data.code == 200){
                        layer.alert(response.data.message,function (){
                            //刷新当前页面
                            window.location.href="${pageContext.request.contextPath}/IndexServlet001";
                        })
                    }else{
                        layer.alert(response.data.message)
                    }


                    console.log(response.data);
                })
                .catch(function (error) {
                    // 请求失败时处理错误
                    console.error(error);
                });
            return false; // 阻止表单默认提交行为
        });

    });

</script>
